package master;

import master.EmployeeDao;
import master.EmployeeDaoImpl;
import employee.Employee;
import user.User;
import user.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;

public class EmployeeService {
    public EmployeeDao employeeDao = new EmployeeDaoImpl();
    public User user = new User();
    // 注册新员工
    public boolean register(Employee employee) {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            int result = employeeDao.addEmployee(conn, employee);
            // 明确判断返回值
            if (result == 1) {
                return true;
            } else if (result == 0) {
                // 用户名已存在
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(conn);
        }
        return false;
    }

    // 检查用户名是否存在
    public boolean isNameExist(String ename) {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            Employee employee = employeeDao.findByName(conn, ename);
            return employee != null;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }

    // 员工登录
    public Employee login(String ename, String password) {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            return employeeDao.login(conn, ename, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
            DBUtil.closeConnection(conn);
        }
    }
}
